Git Kata 6: Remote Repositories

Learn how to create and add remote repositories and push commits to the repository.

The Git katas have, thus far, worked with a local repository. Git has a rich feature set that allows repositories to send and retrieve commits to and from each other, sharing the entire commit history of one repository with an infinite number of other repositories. This kata will demonstrate the steps necessary to connect a local repository to a remote repository.

Step 1: Initialize a new repository#

The command to change the directory and list all the files is given below.

Changing the directory and listing all the files

Commands

Command / Parameter

Description

cd /home/devops/dk/web-storelist

This changes to the web-storelist directory.

ls -A

This lists all the files, including the hidden files and directories.

The web-storelist directory holds an HTML-formatted version of storelist.txt. This will be the content used to publish the grocery list to the web. The ls -A command shows that there’s no hidden .git directory. The web-storelist directory isn’t yet a Git repository.

The command to initialize a new repository is given below:

Initializing an empty Git repositroy

Command's Parameter

Command / Parameter

Description

init

This initializes a new repository.

The git init command creates a new repository in web-storelist.

The commands for repository configuration are given below.

Command's Parameters

Command / Parameter

Description

config

This sets a configuration value that affects the behavior of the Git program.

user.name | user.email

These values set the user name and email identity associated with one or more repositories.

The git config command is used in this step to set the identity for this Git repository.

The command to stage the files is given below.

Staging all the files

Command's Parameters

Command / Parameter

Description

add

This stage changes the working directory to the index. Staged changes will be included in the next commit.

--all

This stages all the modified files.

Recall that git init only creates a repository but doesn’t stage any files or create any commits. The git add command stages files in the working directory, making them ready to be included in the next commit. Remember also that the files in web-storelist are untracked after the repository is created. There are three files in web-storelist, so the --all option stages all three untracked files with one command.

The command to display the status of the repository is given below.

Status of the repository

Command's Parameter

Command / Parameter

Description

status

This displays the status of the files in the working directory of the repository.

The git status command displays the status of the files in web-storelist. The three files in web-storelist are listed as new, staged, and ready for commit.

The output of this command is:

Output

Message

Meaning

On branch master

The current branch is main.

Initial commit

The next commit will be the first commit in this repository.

Changes to be committed: (use "git rm --cached)

The list following this message is the changes that will be committed.

new file: store.png

new file: storelist.htm

new file: style.css

The three changes listed above will be committed. All three files in the working directory have been added. They will be included in the next commit, which will also make them tracked.

The command to commit the changes to the repository is given below:

Committing the changes to the repository

Command's Parameters

Command / Parameter

Description

commit

This creates a new commit in the repository.

-a

This stages all the modified files prior to the commit.

-m

The -m parameter allows the commit message to be defined on the command line instead of opening a text editor.

"Initial Commit"

This is the commit message.

This step creates the first commit in the local web-storelist repository.

The output of this command is:

Output

Message

Meaning

"[master (root-commit) c3ae04e] Initial Commit"

The commit was on the main branch. This commit is the root commit, which is the first commit in the repository. The abbreviated hash of the commit is “b852f3d” (this will be different from the value we see when we execute this command).

"3 files changed, 0 insertions(+)"

This is a summary of the changes included in the commit.

"create mode 100644 store.png"

"create mode 100644 storelist.htm"

"create mode 100644 style.css"

This is a list of all the changes committed. The “create" mode indicates the type of file and its permissions.

Step 2: Add a remote repository#

The command to add a Gogs repository as a remote is given below.

Adding a Gogs repository

Command's Parameters

Command / Parameter

Description

remote

The remote command is used to add, remove, and modify remote repositories to a repository.

add

This indicates that this command will add a new remote to the list of remotes for this repository. A repository can have more than one remote repository set.

origin

This is a name assigned to the remote. By convention, the primary (and often the only) remote is usually named origin.

$EDUCATIVE_LIVE_VM_URL:3000/devops/web-storelist

This is the URL to the remote repository, which is hosted by the Gogs server we ran in the previous kata.

The git remote add command adds a new remote repository to the local web-storelist repository. That is the first step in sharing the commit history between the local repository and another repository.

The URL indicates the path to the empty web-storelist repository created in the Gogs server in the previous kata.

The command to list all the remotes is given below.

Listing all the remotes

Command's Parameters

Command / Parameter

Description

remote

The remote command is used to add, remove, and modify remote repositories to a repository.

-v

This parameter lists all the remotes registered in the current repository.

The git remote -v command lists all the remotes added to the local web-storelist repository. There are two identical URLs, one for fetch and one for push. The fetch and push are Git commands we’ll see in a future kata.

The output of the command is:

Output

Message

Meaning

origin https://ed-6091404232622080:3000/devops/web-storelist (fetch)

This is the URL that will be used by Git for the fetch command when working with the origin remote.

origin https://ed-6091404232622080:3000/devops/web-storelist (fetch)

This is the URL that will be used by Git for the push command when working with the origin remote.

Step 3: Push commits to a remote repository#

The commands to push the commits are given below.

  • Enter “devops” for the username.
  • Enter “katas” for the password.
Pushing commits to the repository

Command's Parameters

Command / Parameter

Description

push

This sends commits on the designated branch to a remote repository.


-u

The `-u` parameter is short for --set-upstream. This option creates a tracking branch in the remote repository. Tracking branches are used by Git to determine if the remote branch is up to date, behind, or ahead of a local branch.

origin

This is the name of the remote to which the pushed commits will be sent.

master

This is the name of the local branch from which the commits are sent.

The git push command sends commits in a local branch to a remote repository designated in the command. Git can push from any repository to any repository accessible by disk or network. Developers typically use a Git server to store a shared codebase. Git servers are also used as the entry point to continuous integration pipelines. This command pushes the local commits in main to the Gogs server we started in the previous Git kata.

This execution creates a new main branch in the remote and sets up a local tracking branch (called origin/main) that’s used to track the differences between the local and remote main branches.

The output of this command is:

Output

Message

Meaning

Counting objects: 100% (3/3), done.

Git counts the number of objects to be sent to the remote repository.

Compressing objects: 100% (2/2), done.

Git compresses objects prior to sending them to the remote, to speed transmission.

Writing objects 100% (3/), 234 bytes | 234.00 KiB/s, done.

Git writes the objects to the remote.

Total 3 (delta 0), reused 0 (delta 0)

A summary of the files sent.

To http://localhost:3000

The URL of the remote to which the commits were sent.


* [new branch] master -> master

This indicates that a new remote branch was created as a result of the command. The main branch didn't exist in the remote until this command was executed. The local main now references the remote origin/main.

The command to display the status of the repository is given below.

Status of the repository

Command's Parameter

Command / Parameter

Description

status

This displays the status of the files in the working directory of the repository.

This execution of git status shows that there are no uncommitted changes in the working directory. There’s an additional line we haven’t seen before, indicating that the local main branch is up-to-date with the tracking branch origin/main. This is the local reference branch we created with the git push -u command earlier.

Pushes to origin from other repositories or new “un-pushed” commits to the local main branch will cause our local main branch to be “ahead” or “behind” the remote. New local commits can be pushed, bringing the local main branch back into synchronization with the remote. Commits to the remote repository from other repositories can be retrieved using git pull, which we’ll see in a later step.

The output of this command is:

Output

Message

Meaning

On branch master

The current branch is main.

Your branch is up-to-date with ‘origin/master’

The commit history of the local main branch matches the commit history of the main branch on the origin remote repository.

nothing to commit, working tree clean

The index of the local repository matches the working directory. There are no unstaged or uncommitted changes.

To view the repository in Gogs:

  • Switch to the browser (Gogs).
  • Click “Dashboard” at the top.
  • Select the “web-storelist” repository under “My Repositories” on the right.
Repository view in Gogs

The git push command has populated the web-storelist repository with the files in the web-storelist local repository. The commit history of this repository can be viewed by clicking the “Commits” link at the center-left. The one commit listed is the one pushed in the previous step.

Git Kata 5: Run a Git Server

Git Kata 7: Collaboration as Ken